package com.zf.yichat.controller.trend;

import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.PageHelper;
import com.zf.yichat.controller.BaseController;
import com.zf.yichat.dto.TrendCommentDto;
import com.zf.yichat.mapper.TrendCommentMapper;
import com.zf.yichat.mapper.TrendMapper;
import com.zf.yichat.mapper.UserMapper;
import com.zf.yichat.model.Trend;
import com.zf.yichat.model.TrendComment;
import com.zf.yichat.model.User;
import com.zf.yichat.service.TrendService;
import com.zf.yichat.service.UserService;
import com.zf.yichat.utils.common.DtoChangeUtils;
import com.zf.yichat.utils.response.FsResponse;
import com.zf.yichat.utils.response.FsResponseGen;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 一句话描述功能
 *
 * @author fengsong
 * @date create in 10:34 2019/9/6 2019
 */
@Controller
@RequestMapping("trend")
public class TrendController extends BaseController {

    @Resource
    private TrendMapper trendMapper;
    @Resource
    private TrendCommentMapper trendCommentMapper;
    @Autowired
    private TrendService trendService;
    @Autowired
    private UserService userService;
    @Resource
    private UserMapper userMapper;
    /**
     * 动态列表
     * @return
     */
    @RequestMapping("index")
    public String index() {
        return "trend/index";
    }

    /**
     * 资讯管理-动态列表
     * @return
     */
    @RequestMapping("official/index")
    public String officialIndex() {
        return "trend/official/index";
    }

    /**
     * 资讯管理-发布资讯
     * @return
     */
    @RequestMapping("official/release")
    public String officialRelease() {
        return "trend/official/add";
    }

    /**
     * 资讯管理-编辑资讯
     * @return
     */
    @RequestMapping("official/edit")
    public String officialEdit() {
        return "trend/official/edit";
    }

    /**
     * 资讯管理-官方用户配置
     * @return
     */
    @RequestMapping("officialUserList")
    public String officialUserList() {
        return "trend/official/user/list";
    }

    /**
     * 资讯管理-官方用户新增
     * @return
     */
    @RequestMapping("officialAddUser")
    public String officialAddUser() {
        return "trend/official/user/add";
    }

    /**
     * 资讯管理-官方用户编辑
     * @return
     */
    @RequestMapping("officialEditUser")
    public String officialEditUser(Long userId) {
        User user = userService.selectById(userId);
        getRequest().setAttribute("user", user);
        return "trend/official/user/edit";
    }

    /**
     * 动态管理-审核
     * @return
     */
    @RequestMapping("examineTrend")
    public String examineTrend() {
        return "trend/examine/index";
    }

    /**
     * 动态评论管理-审核
     * @return
     */
    @RequestMapping("examineTrendComment")
    public String examineTrendComment() {
        return "trend/examine/commentList";
    }


    @RequestMapping("/list")
    @ResponseBody
    public FsResponse list(Integer page, Integer limit, Long userId, Integer publicStatus, String sign) {

        Example example = new Example(Trend.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("status", 0);

        if (Objects.nonNull(userId)) {
            criteria.andEqualTo("userId", userId);
        }
        if (Objects.nonNull(publicStatus)) {
            criteria.andEqualTo("publicStatus", publicStatus);
        }
        if (Objects.nonNull(sign)) {
            criteria.andEqualTo("sign", sign);
        }
        example.setOrderByClause(" order_weight asc, id desc");
//        example.setOrderByClause(" ctime desc");
        PageHelper.startPage(page, limit);
        return DtoChangeUtils.getPageList(trendMapper.selectByExample(example), v -> v);
    }

    /**
     * 查询需要审核的动态
     * @param page
     * @param limit
     * @param userId
     * @param publicStatus
     * @param sign
     * @return
     */
    @RequestMapping("/examineList")
    @ResponseBody
    public FsResponse examineList(Integer page, Integer limit, Long userId, Integer publicStatus, String sign) {

        Example example = new Example(Trend.class);
        Example.Criteria criteria = example.createCriteria();

        if (Objects.nonNull(userId)) {
            criteria.andEqualTo("userId", userId);
        }
        if (Objects.nonNull(publicStatus)) {
            criteria.andEqualTo("publicStatus", publicStatus);
        }
        if (Objects.nonNull(sign)) {
            criteria.andEqualTo("sign", sign);
        }else {
            criteria.orEqualTo("sign", Trend.SIGN_SUBJECT).orEqualTo("sign", Trend.SIGN_INFORMATION);
        }
        example.setOrderByClause(" order_weight asc, id desc");
        PageHelper.startPage(page, limit);

        return DtoChangeUtils.getPageList(trendMapper.selectByExample(example), v -> v);
    }

    /**
     * 查询需要审核的评论
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("/examineCommentList")
    @ResponseBody
    public FsResponse examineCommentList(Integer page, Integer limit) {

        PageHelper.startPage(page, limit);
        List<TrendCommentDto> list = trendService.selectNeedExamineComment(Trend.SIGN_SUBJECT);
        return DtoChangeUtils.getPageList(list, dto -> {
            TrendCommentDto commentDto = new TrendCommentDto();
            BeanUtils.copyProperties(dto, commentDto);
            return commentDto;
        });
    }


    @RequestMapping("/public/set")
    @ResponseBody
    public FsResponse set(Long id) {

        Trend t = new Trend();
        t.setId(id);
        t.setPublicStatus(1);
        trendMapper.updateByPrimaryKeySelective(t);
        return FsResponseGen.success();
    }

    /**
     * 审核功能
     * @param id
     * @return
     */
    @RequestMapping("/public/examine")
    @ResponseBody
    public FsResponse examine(Long id) {
        Trend t = new Trend();
        t.setId(id);
        t.setStatus(Trend.STATUS_EXAMINE_YES);
        trendMapper.updateByPrimaryKeySelective(t);
        return FsResponseGen.success();
    }

    /**
     * 审核功能-审核评论
     * @param id
     * @return
     */
    @RequestMapping("/public/examineComment")
    @ResponseBody
    public FsResponse examineComment(Long id) {
        TrendComment t = new TrendComment();
        t.setId(id);
        t.setStatus(Trend.STATUS_EXAMINE_YES);
        trendCommentMapper.updateByPrimaryKeySelective(t);
        return FsResponseGen.success();
    }


    @RequestMapping("/public/cancle")
    @ResponseBody
    public FsResponse cancle(Long id) {

        Trend t = new Trend();
        t.setId(id);
        t.setPublicStatus(0);
        trendMapper.updateByPrimaryKeySelective(t);
        return FsResponseGen.success();
    }

    /**
     * 删除动态列表
     * @param id
     * @return
     */
    @RequestMapping("/public/delete")
    @ResponseBody
    public FsResponse delete(Long id) {
        trendMapper.deleteByPrimaryKey(id);
        return FsResponseGen.success();
    }

    /**
     * 设置动态列表的排序权重
     * @param id
     * @return
     */
    @RequestMapping("/public/orderWeight")
    @ResponseBody
    public FsResponse orderWeight(Long id, Integer orderWeight) {

        trendMapper.setTrendOrderWeight(id, orderWeight);
        return FsResponseGen.success();
    }

    /**
     * 设置资讯管理员
     * @param nick
     * @return
     */
    @RequestMapping("/setOfficialUser")
    @ResponseBody
    public FsResponse setOfficialUser(String nick, String avatar) {

        User user = new User();
        user.setNick(nick);
        user.setAvatar(avatar);
        user.setRemark(Trend.SIGN_OFFICIAL);
        user.setPassword("123456");
        user.setMobile("18000000000");
        userService.add(user);
        return FsResponseGen.success();
    }

    /**
     * 更新资讯管理员
     * @param nick
     * @return
     */
    @RequestMapping("/updateOfficialUser")
    @ResponseBody
    public FsResponse updateOfficialUser(Long userId, String nick, String avatar) {

        User user = new User();
        user.setId(userId);
        user.setNick(nick);
        user.setAvatar(avatar);
        userService.update(user);
        return FsResponseGen.success();
    }


    /**
     * 后台发布资讯内容
     * @return
     */
    @RequestMapping("/public/release")
    @ResponseBody
    public FsResponse release(String content, String imgs, String videos, String location) {

        // 使用官方账号发布
        Example example = new Example(User.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("remark", Trend.SIGN_OFFICIAL);
        User user = userMapper.selectOneByExample(example);

        List<String> imageList = JSONArray.parseArray(imgs, String.class);
        List<String> videoList = JSONArray.parseArray(videos, String.class);


        trendService.add(content, String.join(",", imageList), String.join(",", videoList), location, user.getId(), Trend.SIGN_OFFICIAL);
        return FsResponseGen.success();
    }


}
